package view;

import bdzeitor.Singleton;
import controller.ContaController;
import domain.Lancamento;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;

public class TelaLancamento extends javax.swing.JFrame {

    public TelaLancamento() throws SQLException {
        this.setTitle("Lançamento");
        initComponents();
        atualizaConta();
        this.jComboConta.setEnabled(false);
        this.jButtonAddConta.setEnabled(false);
        this.jDescricao.setEnabled(true);
        this.jDesconto.setEnabled(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jRadioEntrada = new javax.swing.JRadioButton();
        jRadioSaida = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jDescricao = new javax.swing.JTextField();
        jTipoPag = new javax.swing.JComboBox();
        jValor = new javax.swing.JTextField();
        jJuros = new javax.swing.JTextField();
        jDesconto = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jDtVenc = new com.toedter.calendar.JDateChooser();
        jDataPag = new com.toedter.calendar.JDateChooser();
        jLabel11 = new javax.swing.JLabel();
        jComboConta = new javax.swing.JComboBox();
        jButtonAddConta = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        buttonGroup1.add(jRadioEntrada);
        jRadioEntrada.setSelected(true);
        jRadioEntrada.setText("Entrada");
        jRadioEntrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioEntradaActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioSaida);
        jRadioSaida.setText("Saída");
        jRadioSaida.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioSaidaActionPerformed(evt);
            }
        });

        jLabel1.setText("Descrição:");

        jLabel2.setText("Tipo Pagamento:");

        jLabel4.setText("Valor: R$");

        jLabel5.setText("Juros:");

        jLabel6.setText("Desconto: R$");

        jTipoPag.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "a vista", "a prazo" }));

        jLabel8.setText("Data Vencimento:");

        jLabel9.setText("Data Pagamento:");

        jLabel11.setText("Número da conta:");

        jButtonAddConta.setText("Adicionar Conta");
        jButtonAddConta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAddContaActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/accept_button.png"))); // NOI18N
        jButton1.setText("Salvar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cancel.png"))); // NOI18N
        jButton2.setText("Cancelar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboConta, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonAddConta)
                        .addContainerGap(115, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jDescricao))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTipoPag, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jRadioEntrada)
                                .addGap(62, 62, 62)
                                .addComponent(jRadioSaida))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(jLabel8)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jDtVenc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel9)
                                    .addGap(11, 11, 11)
                                    .addComponent(jDataPag, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel6)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jDesconto))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                            .addComponent(jLabel4)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jLabel5)
                                            .addGap(24, 24, 24)))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jJuros, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jValor, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                        .addGap(37, 37, 37))))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioEntrada)
                    .addComponent(jRadioSaida))
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTipoPag, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jJuros, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jDesconto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jDtVenc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel9)
                    .addComponent(jDataPag, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jComboConta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonAddConta))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        setSize(new java.awt.Dimension(416, 357));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAddContaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAddContaActionPerformed
        TelaPrincipal telaP = Singleton.getInstance();
        TelaConta c = new TelaConta(this);
        telaP.addTela(c.getContentPane(), "Conta");
        
    }//GEN-LAST:event_jButtonAddContaActionPerformed

    private void jRadioEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioEntradaActionPerformed
        this.jComboConta.setEnabled(false);
        this.jButtonAddConta.setEnabled(false);
        this.jDescricao.setEnabled(true);
        this.jDesconto.setEnabled(true);
    }//GEN-LAST:event_jRadioEntradaActionPerformed

    private void jRadioSaidaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioSaidaActionPerformed
        this.jDescricao.setEnabled(false);
        this.jDesconto.setEnabled(false);
        this.jComboConta.setEnabled(true);
        this.jButtonAddConta.setEnabled(true);
    }//GEN-LAST:event_jRadioSaidaActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        Singleton.getInstance().removeTela(this.getClass());
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(jRadioEntrada.isSelected()){
            Lancamento l = new Lancamento();
            l.setValor(jValor.getText());
            l.setDesconto(jDesconto.getText());
            l.setJuros(jJuros.getText());
            l.setDescricao(jDescricao.getText());
            l.setTipoPag(jTipoPag.getSelectedItem().toString());
            l.setValorTotal(String.valueOf(Float.parseFloat(jValor.getText())-Float.parseFloat(jDesconto.getText())+Float.parseFloat(jJuros.getText())));
            l.setDataVencimento(jDtVenc.getDate());
            l.setDataPagamento(jDataPag.getDate());
            l.setTipo("entrada");
            try {
                new ContaController().lancamento(l);
                JOptionPane.showMessageDialog(null, "Lançamento cadastrado com sucesso !!!");
                Singleton.getInstance().removeTela(this.getClass());
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Erro ao salvar o lançamento !!!");
            }
        }
        else{
            Lancamento l = new Lancamento();
            l.setValor(jValor.getText());
            l.setJuros(jJuros.getText());
            l.setTipoPag(jTipoPag.getSelectedItem().toString());
            l.setValorTotal(String.valueOf(Float.parseFloat(jValor.getText())+Float.parseFloat(jJuros.getText())));
            l.setDataVencimento(jDtVenc.getDate());
            l.setDataPagamento(jDataPag.getDate());
            
            ContaController contaC = new ContaController();
            
            try {
                contaC.buscarConta();
                int quantidade = Integer.parseInt(contaC.dao.quantPar.get(jComboConta.getSelectedIndex()));
                System.out.println("teste:" + quantidade);
                l.setQuantidadeParcelas(quantidade);
                l.setIdConta(jComboConta.getSelectedItem().toString());
                l.setTipo("saida");
                new ContaController().lancamento(l);
                JOptionPane.showMessageDialog(null, "Lançamento cadastrado com sucesso !!!");
                Singleton.getInstance().removeTela(this.getClass());
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Erro ao salvar o lançamento !!!");
            }
            
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    
    public void atualizaConta() throws SQLException{
        jComboConta.removeAllItems();
        ContaController contaC = new ContaController();
        contaC.buscarConta();
        ArrayList<String> contas = contaC.dao.listaResult;
        
        for (String conta : contas) {
            jComboConta.addItem(conta);
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaLancamento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaLancamento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaLancamento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaLancamento.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
//                new Lancamento().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButtonAddConta;
    private javax.swing.JComboBox jComboConta;
    private com.toedter.calendar.JDateChooser jDataPag;
    private javax.swing.JTextField jDesconto;
    private javax.swing.JTextField jDescricao;
    private com.toedter.calendar.JDateChooser jDtVenc;
    private javax.swing.JTextField jJuros;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JRadioButton jRadioEntrada;
    private javax.swing.JRadioButton jRadioSaida;
    private javax.swing.JComboBox jTipoPag;
    private javax.swing.JTextField jValor;
    // End of variables declaration//GEN-END:variables
}
